
Linking Diagrams for CS 30200/ECE 46810, Operating Systems

Roger Kraft
Purdue University Calumet


=============================================================================================

1) STATIC LINKING

The file my-library.c contains the definitions of functions that are useful.
The file my-library.h contains declarations for the functions in my-library.h.
The file my-library.obj contains the compiled definitions of the functions from my-library.c.

The file client.c wants to use the functions in my-library.c. So client.c includes
my-library.h and is compiled to client.obj. The file client.obj will contain calls
to functions from my-library, but client.obj does not contain the function
definitions (why?).

To create the executable file client.exe we need to combine the compiled code from
client.obj (with its function calls) and the compiled code from my-library.obj
(with its function definitions). It is the linker program which combines these two
files and "links" the function calls in client.obj with the function definitions in
my-library.obj.

                 ________
                |        | my-library.obj
my-library.c -->|compiler|----------------+
                |________|                |
                   /|\                    |
                    |(sanity check)       |my-library.obj
my-library.h--------+                     |
                    |                     |
                 __\|/___              __\|/___               ________
                |        | client.obj |        | client.exe  |        |   executable image
   client.c --> |compiler|----------->| linker |------------>| loader |-->loaded into
                |________|            |________|             |________|   virtual memory


NOTE: When you use externally supplied libraries, you do not use the whole picture given
      above. The library's source code file need not be supplied by the creators of the
      library (but they must supply the header and obj files).
      In that case, the linking diagram looks more like this.


           their-library.h       their-library.obj
                  |                     |
              ___\|/__               __\|/___               ________
             |        | client.obj  |        | client.exe  |        |   executable image
 client.c -->|compiler|------------>| linker |------------>| loader |-->loaded into
             |________|             |________|             |________|   virtual memory

=============================================================================================

2) STATIC LINK LIBRARIES

A "static link library" generalize the diagram at the top of this page. A "static link library"
is a collection of obj modules combined (not linked) into a single lib file. This combining is
done by a "librarian program" named lib.exe. The following diagram shows how a static link
library (made up of n obj modules) might be created and used.

               ________
              |        |library_1.obj
library_1.c ->|compiler|--------------+
              |________|              |
               ________               |   ___________
              |        |library_2.obj +->|           |
library_2.c ->|compiler|---------------->|lib program|
   .          |________|              +->|___________|
   .           ________               |       |
   .          |        |library_n.obj |       |
library_n.c ->|compiler|--------------+       |
              |________|                      |
                                              |my-library.lib
                                              |
                   my-library.h               |
                       |                      |
                    __\|/___              ___\|/__               ______
                   |        | client.obj |        | client.exe  |      |   executable image
       client.c -->|compiler|----------->| linker |------------>|loader|-->loaded into
                   |________|            |________|             |______|   virtual memory



NOTE: When you use externally supplied libraries you do not use the whole picture given
      above. The library's source code and obj files need not be supplied by the creators
      of the library (but they must supply the header and lib files).
      In that case, the linking diagram looks more like this.


                their-library.h        their-library.lib
                       |                      |
                    __\|/___              ___\|/__               ______
                   |        | client.obj |        | client.exe  |      |   executable image
       client.c -->|compiler|----------->| linker |------------>|loader|-->loaded into
                   |________|            |________|             |______|   virtual memory


NOTE: The "my-library.h" and "their-library.h" headers may in fact each be a number of
      headers. There could be one header for each of the obj modules in the lib file.
      This is in fact the way the C Library works. There is one lib file for the whole
      library (see below) and a large number of header files (the standard C headers).
      On the other hand, Microsoft has just one single header, windows.h, for all of
      its libraries. The windows.h header is a really huge file.


=============================================================================================

3) DYNAMIC LINK LIBRARIES

The above diagrams show how "static link libraries" are created and used. Now let us
look at the more complex, but also much more commonly used, "dynamic link libraries" (dll's).
These are called "dynamic" because the actual linking of the function call to the function
definition is done at runtime (not at link time). In particular, the definition of a
dynamically linked function is NOT in the exe file. There is only enough information in
the exe file for the operating system to find the function's definition (in some dll file).
(For statically linked functions, the function definition has been placed by the linker in
the exe file.)


                                        my-library.def
                                              |
                 ________                  __\|/___
                |        | my-library.obj |        | my-library.dll
my-library.c -->|compiler|--------------->| linker |---------------+
                |________|                |________|               |
                   /|\                        |                    |
                    |(sanity check)           |                    |
my-library.h--------+                         |my-library.lib      |
                    |                         |                    |
                 __\|/___                  __\|/___              _\|/__
                |        |   client.obj   |        | client.exe |      |   executable image
    client.c -->|compiler|--------------->| linker |------------|loader|-->loaded into
                |________|                |________|            |______|   virtual memory


NOTE: When you use externally supplied dll's, you do not use the whole picture given
      above. The library's source code, obj, and def files need not be supplied by the
      creators of the library (but they must supply the header, lib, and dll files).
      In that case, the dynamic linking diagram looks more like this.


           their-library.h       their-library.lib      their-library.dll
                  |                     |                      |
              ___\|/__               __\|/___               __\|/___
             |        | client.obj  |        | client.exe  |        |   executable image
 client.c -->|compiler|------------>| linker |------------>| loader |-->loaded into
             |________|             |________|             |________|   virtual memory


NOTE: These diagrams show a simple example where there is just one library
      that needs to be linked to the client program. In practice, any number
      of library modules can be used to produce the final running process. In
      the case of static linking, all of the library obj modules would be linked
      with the client obj file at the one linker step described above. In the
      case of dynamic linking, each of the library modules would be linked in
      separate steps (to produce each dll), then all of the lib files would be
      linked to the client obj in one step, and then the client exe would be
      loaded with all of the dll's in virtual memory at runtime (draw yourself
      a picture of this in the case of three or four library modules).

      It is also possible to mix static linking with dynamic linking. Try drawing
      a schematic of linking a client program with three static libraries and two
      dynamic libraries.

============================================================================================


NOTE: Notice how lib files for dynamic link libraries and lib files for static link
      libraries play very similar roles in the linking process but they are very
      different kinds of objects produced by different kinds of programs (a linker
      vs. a librarian program).


NOTE: Many commercially available libraries come in both a static and a dynamic
      version. Such a library needs to come with two lib files. They will usually
      have different names. For example, in Microsoft's Visual Studio 6, the file
         C:\Program Files\Microsoft Visual Studio\VC98\Lib\libc.lib
      is the lib file for the static link library version of the C Library, and
         C:\Program Files\Microsoft Visual Studio\VC98\Lib\msvcrt.lib
      is the lib file for the dynamic link library version of the C Library. You
      tell the cl.exe compiler driver which one of these lib files to use by using
      either the \ML or \MD options on the compiler's command line. (There are also
      "debug" versions of these libraries with lib files libcd.lib and msvcrtd.lib
      and command line options \MLd and \MDd.)


NOTE: One last issue that you should think about is where are the header, lib,
      and dll files kept and how do the compiler, linker, and loader find them?
